const express = require('express');
const router = express.Router();
const db = require('../config/db');
const path = require('path');
function checkLogin(req, res, next) {
    if (req.session.user) {
        next();
    } else {
        res.redirect('/auth/login');
    }
}
router.get('/', checkLogin, (req, res) => {
    res.sendFile(path.join(__dirname, '../public/user.html'));
});
router.get('/address', checkLogin, (req, res) => {
    res.sendFile(path.join(__dirname, '../public/address.html'));
});
router.get('/get-addresses', checkLogin, (req, res) => {
    const userId = req.session.user.id;
    db.query('SELECT * FROM addresses WHERE user_id = ?', [userId], (err, results) => {
        if (err) throw err;
        res.json(results);
    });
});
router.post('/add-address', checkLogin, (req, res) => {
    const userId = req.session.user.id;
    const { address } = req.body;
    db.query('INSERT INTO addresses (user_id, address) VALUES (?, ?)', [userId, address], (err, result) => {
        if (err) throw err;
        res.redirect('/user/address');
    });
});
router.get('/manage', checkLogin, (req, res) => {
    res.sendFile(path.join(__dirname, '../public/manage.html'));
});
router.get('/cart', checkLogin, (req, res) => {
    res.sendFile(path.join(__dirname, '../public/cart.html'));
});

module.exports = router;